import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

const String _content = "测试内容";

Future<Thought> createThought(
        {String content = _content, int id = idTest}) async =>
    await thoughtTable.create({"content": content, "user_id": id});

final Duration dura = Duration(minutes: 1);
final DateTime now = DateTime.now();

void main() {
  /// 创建一个独立的数据库
  prepareAndCleanPostgres("thoughts_test_db");

  test("创建新想法", () async {
    // 创建一个新的想法
    final thought = await createThought();
    // id从1开始
    expect(thought.id, 1);
    expect(thought.content, _content);
    expect(thought.timestamp.isNear(DateTime.now()), isTrue);
  });

  test("寻找所有想法", () async {
    // 原来的想法数量为0
    List<Thought> before = await thoughtTable.findFor(idTest);
    expect(before, isEmpty);
    // 创建2个新想法，数量变为2
    await createThought();
    await createThought();
    List<Thought> after = await thoughtTable.findFor(idTest);
    expect(after, hasLength(2));
  });

  test("单个想法寻找", () async {
    // 插入一个想法，能够根据id号和时间戳找到此想法
    await createThought();
    final Thought? found = await thoughtTable.find(1);
    expect(found, isNotNull);
    expect(found?.content, _content);
  });

  test("查找某用户的想法", () async {
    // 给不同的用户插入相同的想法，根据其中一个用户查找想法，能找到2个
    await createThought();
    await createThought();
    // 创建新用户，并使用id2来创建新想法
    await userTable.create({"phone": "15648875254"});
    await createThought(id: 2);
    List<Thought> founds = await thoughtTable.findFor(idTest);
    expect(founds, hasLength(2));
    expect(founds.first.content, _content);
  });

  test("按时间范围查找", () async {
    // 原来按时间范围无法找到
    List<Thought> foundsBefore = await thoughtTable.findBetween(
      Period(now.subtract(dura), now.add(dura)),
      userId: idTest,
    );
    expect(foundsBefore, isEmpty);
    // 创建一个新想法后重新寻找，可以找到1个
    await createThought();
    List<Thought> foundsAfter = await thoughtTable.findBetween(
      Period(now.subtract(dura), now.add(dura)),
      userId: idTest,
    );
    expect(foundsAfter, hasLength(1));
    // 在之前或之后超出范围查找，都无法找到
    List<Thought> foundsNone1 = await thoughtTable.findBetween(
      Period(now.subtract(dura * 2), now.subtract(dura)),
      userId: idTest,
    );
    expect(foundsNone1, isEmpty);
    List<Thought> foundsNone2 = await thoughtTable.findBetween(
      Period(now.add(dura), now.add(dura * 2)),
      userId: idTest,
    );
    expect(foundsNone2, isEmpty);
  });

  test("更新想法", () async {
    // 插入一个想法，并修改想法内容
    final Thought thought = await createThought();
    final time = DateTime.now().subtract(Duration(days: 1));
    thought.content = "修改后";
    thought.timestamp = time;
    await thoughtTable.update(thought);
    // 根据同样的条件查询想法，能够得到修改后的内容
    final Thought? found = await thoughtTable.find(thought.id);
    expect(found, isNotNull);
    expect(found?.content, "修改后");
    expect(found?.timestamp, time);
  });

  test("空修改实验", () async {
    // 直接修改一个想法，得到报错
    final touhgt = Thought(id: 1, timestamp: DateTime.now(), content: "");
    expect(() async => await thoughtTable.update(touhgt), isBug);
  });
}
